/*********************************************************************
 Copyright 2014 the Flapi authors

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 ********************************************************************/

package unquietcode.tools.flapi.graph.components;


import java.util.Set;
import java.util.TreeSet;

import static com.google.common.base.Preconditions.checkState;

/**
 * @author Ben Fagin
 * @version 07-10-2012
 */
public class StateClass {
	protected Set<Transition> transitions = new TreeSet<Transition>();
	private Object blockMarker;
	private String name;
	private boolean isTopLevel = false;
	private final Class<?> helperClass;
	private final Class<?> beanClass;

	public StateClass() {
		this(null, null);
	}

	public StateClass(Class<?> helperClass, Class<?> beanClass) {
		this.helperClass = helperClass;
		this.beanClass = beanClass;
	}

	public Class<?> getHelperClass() {
		return helperClass;
	}

	public Class<?> getBeanClass() {
		return beanClass;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public boolean isTopLevel() {
		return isTopLevel;
	}

	public void setIsTopLevel() {
		isTopLevel = true;
	}


	// for debugging porpoises, mostly
	@Override
	public String toString() {
		String str = "";
		for (Transition transition : getTransitions()) {
			str += transition.getMethodSignature() + " | ";
		}

		return str;
	}

	public TreeSet<Transition> getTransitions() {
		return new TreeSet<Transition>(transitions);
	}

//	public void replaceTransition(Transition theOldOne, Transition theNewOne) {
//		checkNotNull(theOldOne);
//		checkNotNull(theNewOne);
//		checkState(transitions.contains(theOldOne), "the transition to replace was not found");
//
//		transitions.remove(theOldOne);
//		addTransitions(theNewOne);
//	}

	public final void addTransitions(Transition...transitions) {
		for (Transition transition : transitions) {
			checkState(transition.getOwner() == null, "Transition should not have an owner initially.");
			transition.setOwner(this);
			this.transitions.add(transition);
		}
	}

	public final Object getBlockMarker() {
		return blockMarker;
	}

	public final void setBlockMarker(Object marker) {
		this.blockMarker = marker;
	}

	public boolean isLateral(StateClass other) {
		return this == other                                    // true of we're the same object
			|| this.getBlockMarker() == other.getBlockMarker()  // true if we are from the same block
		;
	}
}
